package com.briup.day16;

/**
 * @author ThinkBook
 */
public class Test04_String {
    //String面试题
    public static void main(String[] args) {
        String s1 = "a";
        String s2 = "b";

        // 常量优化机制："a" 和 "b"都是字面值常量，借助 + 连接，其结果 "ab" 也被当作常量
        String s3 = "a" + "b";
        String s4 = "ab";

        System.out.println(s3.equals(s4));	// true
        System.out.println(s3 == s4);		// true

        System.out.println("-------------");

        String s5 = s1 + s2;
        System.out.println(s4.equals(s5));	// true
        System.out.println(s4 == s5);		// false

        System.out.println("-------------");

        String s6 = (s1 + s2).intern();
        System.out.println(s4.equals(s6));	// true
        System.out.println(s4 == s6);		// true
    }
    //String常量池
    public static void main02(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        // 字符串 也存在 常量池
        System.out.println(s1 == s2);   //true

        String s3 = new String("hello");
        String s4 = new String("hello");
        System.out.println(s3 == s4);   //false

        System.out.println(s1 == s3);   //false

        System.out.println("--------");

        String s5 = new String("hello").intern();
        System.out.println(s1 ==s5);
    }
    public static void main01(String[] args) {
        String s1 = "abc";
        String s2 = new String();
        String s3 = new String("hello");
        char[] arr = {'w','t','o'};
        String s4 = new String(arr);

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }
}
